package com.cdpm.web.controller.common;

import com.cdpm.common.core.controller.BaseController;
import com.cdpm.common.core.page.TableDataInfo;
import com.cdpm.common.utils.CollectionUtil;
import com.cdpm.common.utils.DateUtils;
import com.cdpm.common.utils.StringUtils;
import com.cdpm.common.utils.bean.BeanUtils;
import com.cdpm.system.domain.PrizeConfig;
import com.cdpm.system.service.IPrizeService;
import com.cdpm.web.dto.PrizeConfigDTO;
import com.cdpm.web.dto.req.InsertOrUpdatePrizeReqDTO;
import com.cdpm.web.dto.req.InsertOrUpdatePrizeReqListDTO;
import com.cdpm.web.dto.req.QueryPrizeByIdReqDTO;
import com.cdpm.web.dto.req.QueryPrizeListReqDTO;
import com.cdpm.web.dto.res.InsertOrUpdatePrizeResDTO;
import com.cdpm.web.dto.res.QueryPrizeByIdResDTO;
import com.cdpm.web.dto.res.QueryPrizeListResDTO;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 奖品配置
 */
@RestController
@RequestMapping("/prize")
public class PrizeController extends BaseController {
    @Resource
    IPrizeService IPrizeService;
    @PostMapping("/queryPrizeList")
    public QueryPrizeListResDTO queryPrizeList(@RequestBody QueryPrizeListReqDTO QueryPrizeListReqDTO){
        QueryPrizeListResDTO QueryPrizeListResDTO = new QueryPrizeListResDTO();
        startPage();
        List<PrizeConfigDTO> list = new ArrayList<>();
        PrizeConfig PrizeConfig = new PrizeConfig();
        BeanUtils.copyBeanProp(PrizeConfig,QueryPrizeListReqDTO);
        PrizeConfig.setIsDelete("0");
        if(QueryPrizeListReqDTO.getActivityId()!=null){
            PrizeConfig.setActivityId(Long.parseLong(QueryPrizeListReqDTO.getActivityId()));
        }
        List<PrizeConfig> PrizeConfigs = IPrizeService.findList(PrizeConfig);
        TableDataInfo dataTable = getDataTable(PrizeConfigs);
        QueryPrizeListResDTO.setRows(dataTable.getRows());
        QueryPrizeListResDTO.setTotal(dataTable.getTotal());
        QueryPrizeListResDTO.setMsg(dataTable.getMsg());
        QueryPrizeListResDTO.setCode(dataTable.getCode());
        if(CollectionUtil.isNotEmpty(QueryPrizeListResDTO.getRows())){
            List<PrizeConfig> PrizeConfigs1 = (List<PrizeConfig>)QueryPrizeListResDTO.getRows();
            for (PrizeConfig PrizeConfig1 : PrizeConfigs1) {
                PrizeConfigDTO PrizeConfigDTO = new PrizeConfigDTO();
                BeanUtils.copyBeanProp(PrizeConfigDTO,PrizeConfig1);
                PrizeConfigDTO.setInsertTimeStr(DateUtils.parseDateToStr(PrizeConfigDTO.getInsertTime(),"yyyy-MM-dd HH:mm:ss"));
                PrizeConfigDTO.setUpdateTimeStr(DateUtils.parseDateToStr(PrizeConfigDTO.getUpdateTime(),"yyyy-MM-dd HH:mm:ss"));
                
                if(PrizeConfig1.getPrizeId()!=null){
                    PrizeConfigDTO.setPrizeId(PrizeConfig1.getPrizeId().toString());
                }
                if(PrizeConfig1.getPrizeId()!=null){
                    PrizeConfigDTO.setPrizeId(PrizeConfig1.getPrizeId().toString());
                }
                list.add(PrizeConfigDTO);
            }
        }
        QueryPrizeListResDTO.setRows(list);
        return QueryPrizeListResDTO;
    }

    /**
     * 活动配置详情查询
     */
    @PostMapping("/queryPrizeById")
    public QueryPrizeByIdResDTO queryPrizeById(@RequestBody QueryPrizeByIdReqDTO QueryPrizeByIdReqDTO){
        QueryPrizeByIdResDTO QueryPrizeByIdResDTO = new QueryPrizeByIdResDTO();
        long PrizeId = Long.parseLong(QueryPrizeByIdReqDTO.getPrizeId());
        PrizeConfig PrizeConfig = IPrizeService.findById(PrizeId);
        if(PrizeConfig!=null){
            BeanUtils.copyBeanProp(QueryPrizeByIdResDTO,PrizeConfig);
            QueryPrizeByIdResDTO.setPrizeId(PrizeConfig.getPrizeId()+"");
            QueryPrizeByIdResDTO.setInsertTimeStr(DateUtils.parseDateToStr(PrizeConfig.getInsertTime(),"yyyy-MM-dd HH:mm:ss"));
            QueryPrizeByIdResDTO.setUpdateTimeStr(DateUtils.parseDateToStr(PrizeConfig.getUpdateTime(),"yyyy-MM-dd HH:mm:ss"));
            
            if(PrizeConfig.getPrizeId()!=null){
                QueryPrizeByIdResDTO.setPrizeId(PrizeConfig.getPrizeId().toString());
            }
            if(PrizeConfig.getPrizeId()!=null){
                QueryPrizeByIdResDTO.setPrizeId(PrizeConfig.getPrizeId().toString());
            }
        }
        return QueryPrizeByIdResDTO;
    }

    /**
     * 新增或更新活动配置
     */
    @PostMapping("/insertOrUpdatePrize")
    public InsertOrUpdatePrizeResDTO insertOrUpdatePrize(@RequestBody InsertOrUpdatePrizeReqDTO InsertOrUpdatePrizeReqDTO){
        InsertOrUpdatePrizeResDTO insertOrUpdatePrizeResDTO = new InsertOrUpdatePrizeResDTO();
        PrizeConfig PrizeConfig = new PrizeConfig();
        BeanUtils.copyBeanProp(PrizeConfig,InsertOrUpdatePrizeReqDTO);

        if(StringUtils.isNotEmpty(InsertOrUpdatePrizeReqDTO.getPrizeId())){
            PrizeConfig.setPrizeId(Long.parseLong(InsertOrUpdatePrizeReqDTO.getPrizeId()));
        }
        if(StringUtils.isNotEmpty(InsertOrUpdatePrizeReqDTO.getPrizeId())){
            //更新
            IPrizeService.update(PrizeConfig);
        }else{
            //新增
            PrizeConfig.setIsDelete("0");
            IPrizeService.insert(PrizeConfig);
        }
        return insertOrUpdatePrizeResDTO;
    }

    @PostMapping("/insertOrUpdatePrizeList")
    public InsertOrUpdatePrizeResDTO insertOrUpdatePrizeList(@RequestBody InsertOrUpdatePrizeReqListDTO prizes){

        InsertOrUpdatePrizeResDTO insertOrUpdatePrizeResDTO = new InsertOrUpdatePrizeResDTO();
        try {
            PrizeConfig prizeConfig = new PrizeConfig();
            prizeConfig.setActivityId(Long.parseLong(prizes.getActivityId()));
            List<PrizeConfig> list = IPrizeService.findList(prizeConfig);
            for(PrizeConfig p:list){
                p.setIsDelete("1");
                IPrizeService.update(p);
            }
            for(InsertOrUpdatePrizeReqDTO p:prizes.getPrizes()){
                PrizeConfig PrizeConfig = new PrizeConfig();
                BeanUtils.copyBeanProp(PrizeConfig,p);
                PrizeConfig.setIsDelete("0");
                PrizeConfig.setActivityId(Long.parseLong(prizes.getActivityId()));
                IPrizeService.insert(PrizeConfig);
            }
            insertOrUpdatePrizeResDTO.setCode(200);
            insertOrUpdatePrizeResDTO.setMsg("SUCESS");
        } catch (NumberFormatException e) {
            insertOrUpdatePrizeResDTO.setCode(500);
            insertOrUpdatePrizeResDTO.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return insertOrUpdatePrizeResDTO;
    }



}
